#include <jni.h>
#include <iostream>
#include <boost\locale.hpp>
#include <boost\config.hpp>
#include "SerialPortFactory.h"

JavaVM* g_jvm;
std::string encode;
extern boost::shared_ptr<SerialPortFactory> serialPortFactory;

JNIEXPORT jint JNICALL

JNI_OnLoad(JavaVM *vm, void *reserved) {
	encode = boost::locale::util::get_system_locale(false);

	int index = encode.find(".");
	
	LOGD("%s,%d",encode.c_str(), index);
	
	if (index < 0) {
		//std::cout << "encode :" << encode << "not sure" << std::endl;
		LOGD("encode:%s not sure.use utf-8 as default.",encode.c_str());
		encode = "UTF-8";
		encode = "UTF-8";
	}
	else {
		encode = encode.substr(index + 1, encode.length() - index);
		LOGD("encode:%s",encode.c_str());
	}

	//std::cout << "jni started.encode:"<< encode << std::endl;
	g_jvm = vm;
	return JNI_VERSION_1_6;
}

JNIEXPORT void JNICALL
JNI_OnUnload(JavaVM *vm, void *reserved) {
	std::cout << "unload:" << encode << std::endl;
	g_jvm = NULL;
	serialPortFactory->uninit();
	serialPortFactory.reset();
}